package com.euphoric.version.ant;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import com.euphoric.version.ant.util.SourceWriter;
import com.euphoric.version.ant.util.VersionFactory;

public class IncrementTask extends Task {

	private static final String SERIAL = "\\s*private\\s*static\\s*final\\s*long\\s*" +
			"serialVersionUID\\s*=\\s*([-]?[0-9]*)L;.*";
	
	private String name;
	private File sourceFile;
	private String className;
	private String packageName;
	private String prefix;
	
	public void setSource(File sourceFile) {
		this.sourceFile = sourceFile;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setClass(String className) {
		this.className = className;
	}

	public void setPackage(String packageName) {
		this.packageName = packageName;
	}
	
	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	private String getRegex(String var) {
		return "\\s*protected\\s*int\\s*" + var + "\\s*=\\s*([0-9]*);.*";
	}
	 
	@Override
	public void execute() throws BuildException {
		VersionFactory version;
		
		if (!sourceFile.exists()) {
			version = new VersionFactory(name);
			SourceWriter.create(sourceFile, version, className, packageName);
			
		} else {
			try {
				BufferedReader reader = new BufferedReader(new FileReader(sourceFile));
				String line;
				
				int maj = 1;
				int min = 0;
				int rev = 0;
				int bld = 1;
				
				long serial = 0L;
				
				while ((line = reader.readLine()) != null) {
					int found = 0;
					
					if (line.matches(getRegex("majorVersion"))) {
						maj = Integer.parseInt(line.replaceFirst(getRegex("majorVersion"), "$1"));
						found++;
					}
					if (line.matches(getRegex("minorVersion"))) {
						min = Integer.parseInt(line.replaceFirst(getRegex("minorVersion"), "$1"));
						found++;
					}
					if (line.matches(getRegex("revision"))) {
						rev = Integer.parseInt(line.replaceFirst(getRegex("revision"), "$1"));
						found++;
					}
					if (line.matches(getRegex("buildNumber"))) {
						bld = Integer.parseInt(line.replaceFirst(getRegex("buildNumber"), "$1"));
						found++;
					}
					if (line.matches(SERIAL)) {
						serial = Long.parseLong(line.replaceFirst(SERIAL, "$1"));
						found++;
					}
					
					if (found == 5) break;
				}
				
				reader.close();
				version = increment(maj, min, rev, bld, serial, name);
				SourceWriter.create(sourceFile, version, className, packageName);
				
			} catch (Exception ex) {
				ex.printStackTrace();
				throw new BuildException("Update Failed");
			}
			
		}

		getProject().setNewProperty(prefix + ".major", String.valueOf(version.getMajorVersion()));
		getProject().setNewProperty(prefix + ".minor", String.valueOf(version.getMinorVersion()));
		getProject().setNewProperty(prefix + ".revision", String.valueOf(version.getRevision()));
		getProject().setNewProperty(prefix + ".build", String.valueOf(version.getBuildNumber()));
		getProject().setNewProperty(prefix + ".version", String.valueOf(version.getVersion()));
		getProject().setNewProperty(prefix + ".full", String.valueOf(version.getFullVersion()));
	}
	
	public VersionFactory increment(int maj, int min, int rev, int bld, long serial, String name) {
		return new VersionFactory(maj, min, rev, (bld + 1), serial, name);
	}
}
